module.exports = function ($resource, $state, $http, $q, toaster, $modal) {
	return {
		restrict: 'AE',
		template: require('../views/productshopOne.html'),
		replace: true,
		scope: {
			'saleobj': '=',
			'util': '=',
		},
		link: function (scope, elements, attrs) {
			scope.isLoading = false;
			scope.tickettypeobj = {
				'id': '',
				'name': scope.saleobj.name,
				'sale_code': scope.saleobj.code,
				'place_code': '',
				'periodstart': '',
				'periodend': '',
				'type': 'D',
				'is_live': '0',
				'appoint': '0'
			};
			scope.periodstart =
				{
					'lable': scope.util.date2str(new Date()),
					'opened': false
				};
			scope.periodend =
				{
					'lable': scope.util.date2str(new Date(new Date().getFullYear(), 11, 31)),
					'opened': false
				};
			//页面需要的数据
			scope.page = {};

			//获取景点列表
			scope.getCreatePlace = function(){
				$resource('/api/ac/tc/officeBindService/getCreatePlaceList', {}, {}).save({'type': 'D'}, function(res){
					if (res.errcode !== 0) {
						toaster.error({ title: "提示", body: res.errmsg });
						return;
					}
					scope.page.viewlist = res.data;
				});
			}

			scope.load = function () {
				scope.getCreatePlace();
				$resource('/api/as/tc/salettype/list', {}, {}).save({ 'sale_code': scope.saleobj.code }, function (res) {
					if (res.errcode !== 0) {
						toaster.error({ title: "提示", body: res.errmsg });
						return;
					}
					scope.shopArr = res.data;
					if (scope.shopArr.length == 1) {
						scope.tickettypeobj = scope.shopArr[0]
						scope.tickettypeobj.place_code = scope.shopArr[0].view_code
						scope.periodstart.lable = scope.shopArr[0].periodstart
						scope.periodend.lable = scope.shopArr[0].periodend
					} else if (scope.shopArr.length > 1) {
						toaster.error({ title: "提示", body: '数据错误,门店数大于一条' });
						return;
					}
				});
			}
			scope.load();

			scope.save = function () {
				if (!scope.tickettypeobj.view_code) {
					toaster.warning({ title: '', body: '未选择景区/门店，无法保存' });
					return ;
				}
				scope.isLoading = true;
				var periodstart = "";
				var periodend = "";
				if (typeof scope.periodstart.lable === 'string') {
					periodstart = scope.periodstart.lable;
				} else {
					periodstart = scope.util.date2str(scope.periodstart.lable);
				}
				if (typeof scope.periodend.lable === 'string') {
					periodend = scope.periodend.lable;
				} else {
					periodend = scope.util.date2str(scope.periodend.lable);
				}
				var para = {
					'id': scope.tickettypeobj.id,
					'name': scope.tickettypeobj.name,
					'sale_code': scope.tickettypeobj.sale_code,
					'place_code': scope.tickettypeobj.view_code,
					'periodstart': periodstart,
					'periodend': periodend,
					'type': 'M',
					'appoint': scope.tickettypeobj.appoint
				};
				$resource('/api/as/tc/salettype/save', {}, {})
					.save(para, function (res) {
						if (res.errcode !== 0) {
							toaster.error({ title: "提示", body: res.errmsg });
							return;
						}
						toaster.success({ title: "提示", body: '修改成功' });
						// gettickettypedetail();
						scope.load();
						scope.isLoading = false;
					});
			};
			scope.dateOpen = function ($event, item) {
				$event.preventDefault();
				$event.stopPropagation();
				item.opened = true;
			};

			scope.createView = function () {
				var modalInstance = $modal.open({
					template: require('../views/createView.html'),
					controller: 'createView',
					size: 'lg',
					resolve: {
						type: function () {
							return "D"
						}
					}
				});
				//关闭模态框刷新页面
				modalInstance.result.then(function (result) {
					scope.getCreatePlace();
				}, function () {
					scope.getCreatePlace();
				});
			}

		}
	};
};

